package org.loofer.uiframework.navigator.backstack;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

public class AbstractActivity extends AppCompatActivity {
    private static final String TAG = "AbstractActivity";
    protected FragmentActivityBackHelper mFragmentActivityBackHelper = new FragmentActivityBackHelper(this);

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean result;
        if (4 == keyCode) {
            result = this.mFragmentActivityBackHelper.onBackPressed();
        } else {
            result = this.mFragmentActivityBackHelper.onKeyDown(keyCode, event);
        }
        return result || super.onKeyDown(keyCode, event);
    }

    public String getParam(String key) {
        Intent intent = getIntent();
        if (intent != null) {
            return intent.getStringExtra(key);
        }
        return null;
    }

    public void setLaunchFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) {
        this.mFragmentActivityBackHelper.setLaunchFragmentAttr(containerViewRes, enterAnimRes, popExitAnimRes);
    }

    public final void launchRootFragment(AbstractFragment fragment) {
        this.mFragmentActivityBackHelper.launchRootFragment(fragment);
    }

    public AbstractFragment getRootFragment() {
        return this.mFragmentActivityBackHelper.getRootFragment();
    }

    protected void onDestroy() {
        super.onDestroy();
        this.mFragmentActivityBackHelper.clearFragmentBackStack();
    }

    public FragmentActivityBackHelper getFragmentActivityBackHelper() {
        return this.mFragmentActivityBackHelper;
    }

    protected final boolean isFragmentBackStackEmpty() {
        return this.mFragmentActivityBackHelper.isFragmentBackStackEmpty();
    }

    public final void launchFragment(AbstractFragment fragment) {
        this.mFragmentActivityBackHelper.launchFragment(fragment);
    }

    public final void launchFragment(AbstractFragment fragment, int containId, int enterAnimRes, int popExitAnimRes, int launchFlag) {
        this.mFragmentActivityBackHelper.launchFragment(fragment, containId, enterAnimRes, popExitAnimRes, launchFlag);
    }

    public final void popTopFragment() {
        this.mFragmentActivityBackHelper.finishTopFragment();
    }

    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }

    public AbstractFragment getTopFragment() {
        return this.mFragmentActivityBackHelper.getTopFragment();
    }

    public FragmentBackStackManager getFragmentBackStackManager() {
        return this.mFragmentActivityBackHelper.getFragmentBackStackManager();
    }

    public void onBackPressed() {
        if (!this.mFragmentActivityBackHelper.onBackPressed()) {
            super.onBackPressed();
        }
    }
}
